/* Table 7:  STRUCTURE IN VOTING ON LOCAL BALLOT MEASURES, WITH MEASURES ON SAME/SIMILAR ISSUES */

set linesize 250
local PATH_TBL = "~/Downloads/tables"
local PATH_NUM = "~/Downloads/numbers"



/* Local Education Ballot Measures */
 use tb7_long_local_ballot_measures_education_extract, clear

  gen byte Y = (choice == "Y") if (choice == "Y" | choice == "N")
  keep state county place n cvr_id pid_num Y
  egen x = count(n), by(state county place cvr_id n)
  drop if x == 2
  drop x

  reshape wide Y, i(state county place cvr_id) j(n) // reshape so you link items on the same ballot as columns
  drop if pid_num == 0
  replace pid_num = 0 if pid_num == -1
  gen Y_avg = Y2 // or egen Y_avg = rmean(Y2 Y3 Y4 Y5)
  gen x = state + " " + place
  encode x, gen(place_id)
  drop x
  save tmp_tb7, replace

  drop if Y_avg == . | pid_num == .
  corr Y1 Y_avg pid_num

  /* Local Education Regressions */
  xtset place_id

  xtreg Y1          pid_num, fe vce(cluster place_id)
   local b_1_p =  _b[pid_num]
   local s_1_p = _se[pid_num]
   local r_1   = e(r2_w)
   local n_1   = e(N)
  xtreg Y1    Y_avg        , fe vce(cluster place_id)
   local b_2_y =  _b[Y_avg]
   local s_2_y = _se[Y_avg]
   local r_2   = e(r2_w)
   local n_2   = e(N)
  xtreg Y1    Y_avg pid_num, fe vce(cluster place_id)
   local b_3_p =  _b[pid_num]
   local s_3_p = _se[pid_num]
   local b_3_y =  _b[Y_avg]
   local s_3_y = _se[Y_avg]
   local b_3   =  _b[Y_avg]
   local s_3   = _se[Y_avg]
   local r_3   = e(r2_w)
   local n_3   = e(N)


 /* State Partisan Regressions */
use tb7_long_state_partisan_races_education_extract.dta, clear

  merge m:1 state county cvr_id using  tmp_tb7
  keep if _merge == 3
  drop if pid_num == 0
  replace pid_num = 0 if pid_num == -1
  gen byte D = (choice == "D") if (choice == "D" | choice == "R")

  drop if Y_avg == . | pid_num == .
  xtset item_id_2

  xtreg D          pid_num, fe vce(cluster item_id_2)
   local b_6_p =  _b[pid_num]
   local s_6_p = _se[pid_num]
   local r_6   = e(r2_w)
   local n_6   = e(N)
  xtreg D    Y_avg        , fe vce(cluster item_id_2)
   local b_7_y =  _b[Y_avg]
   local s_7_y = _se[Y_avg]
   local r_7   = e(r2_w)
   local n_7   = e(N)
  xtreg D    Y_avg pid_num, fe vce(cluster item_id_2)
   local b_8_p =  _b[pid_num]
   local s_8_p = _se[pid_num]
   local b_8_y =  _b[Y_avg]
   local s_8_y = _se[Y_avg]
   local b_8   =  _b[Y_avg]
   local s_8   = _se[Y_avg]
   local r_8   = e(r2_w)
   local n_8   = e(N)

  quietly {
    capture log close
    log using "`PATH_TBL'/table_7.tex", replace text
    noisily display "\begin{table}[htpb] "
    noisily display "\centering \small"
    noisily display "\begin{threeparttable} "
    noisily display "\caption{\bf Voting Patterns by Partisanship, Regression Estimates} "
    noisily display "\label{table_7} "
    noisily display "\begin{tabular}{l r r r r r} "
    noisily display "\toprule\toprule "
    noisily display "                        &           & Education  & Within    &       \\ "
    noisily display "Outcome                 & Democrat  & Spending 2 & R-square  & Obs.  \\ "
    noisily display "\midrule "
    noisily display "Education Spending 1    &  " %5.3f `b_1_p' "  &  "               "  &  " %5.3f `r_1' " & " %12.0fc `n_1' " \\ "
    noisily display "                        & (" %5.3f `s_1_p' ") &  "               "  &  "             " & "               " \\ [.02in] "
    noisily display "Education Spending 1    &  "               "  &  " %5.3f `b_2_y' "  &  " %5.3f `r_2' " & " %12.0fc `n_2' " \\ "
    noisily display "                        &  "               "  & (" %5.3f `s_2_y' ") &  "             " & "               " \\ [.02in] "
    noisily display "Education Spending 1    &  " %5.3f `b_3_p' "  &  " %5.3f `b_3_y' "  &  " %5.3f `r_3' " & " %12.0fc `n_3' " \\ "
    noisily display "                        & (" %5.3f `s_3_p' ") & (" %5.3f `s_3_y' ") &  "             " & "               " \\ [.02in] "
    noisily display "\midrule "
    noisily display "State Partisan Offices  &  " %5.3f `b_6_p' "  &  "               "  &  " %5.3f `r_6' " & " %12.0fc `n_6' " \\ "
    noisily display "                        & (" %5.3f `s_6_p' ") &  "               "  &  "             " & "               " \\ [.02in] "
    noisily display "State Partisan Offices  &  "               "  &  " %5.3f `b_7_y' "  &  " %5.3f `r_7' " & " %12.0fc `n_7' " \\ "
    noisily display "                        &  "               "  & (" %5.3f `s_7_y' ") &  "             " & "               " \\ [.02in] "
    noisily display "State Partisan Offices  &  " %5.3f `b_8_p' "  &  " %5.3f `b_8_y' "  &  " %5.3f `r_8' " & " %12.0fc `n_8' " \\ "
    noisily display "                        & (" %5.3f `s_8_p' ") & (" %5.3f `s_8_y' ") &  "             " & "               " \\ [.02in] "
    noisily display "\bottomrule "
    noisily display "\end{tabular}"
    noisily display "\begin{tablenotes} "
    noisily display "\footnotesize \item Each row is a separate regression. Standard errors, clustered by contests, are in parentheses. "
    noisily display "Fixed effects for contests included in all cases.  "
    noisily display "\end{tablenotes} "
    noisily display "\end{threeparttable} "
    noisily display "\end{table} "
    log close
  }

  quietly {
    capture log close
    log using "`PATH_NUM'/no_educ_1.tex", replace text
    noisily display %4.2f `r_1'
    log close
  }


 ** robustness checks not shown
 /*
  xtreg Y1    Y_avg        if pid_num == 1, fe vce(cluster place_id)
   local b_4_y =  _b[Y_avg]
   local s_4_y = _se[Y_avg]
   local r_4   = e(r2_w)
   local n_4   = e(N)
  xtreg Y1    Y_avg        if pid_num == 0, fe vce(cluster place_id)
   local b_5_y =  _b[Y_avg]
   local s_5_y = _se[Y_avg]
   local r_5   = e(r2_w)
   local n_5   = e(N)

*    noisily display "\midrule "
*    noisily display "Education Spending 1,  &  "               "  &  " %5.3f `b_4_y' "  &  " %5.3f `r_4' " & " %12.0fc `n_4' " \\ "
*    noisily display "\quad Democrats        &  "               "  & (" %5.3f `s_4_y' ") &  "             " & "               " \\ [.02in] "
*    noisily display "Education Spending 1,  &  "               "  &  " %5.3f `b_5_y' "  &  " %5.3f `r_5' " & " %12.0fc `n_5' " \\ "
*    noisily display "\quad Republicans      &  "               "  & (" %5.3f `s_5_y' ") &  "             " & "               " \\ [.02in] "

*/
